/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pe.com.transportes.dao;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import pe.com.transportes.modelo.Bus;

/**
 *
 * @author z3phiroth
 */
public class BusDAO {

    public void insertarbus(Connection con,Bus c) throws SQLException, FileNotFoundException{

    String sql ="insert into bus values (?,?,?,?,?,?,?)";
    PreparedStatement ps = con.prepareStatement(sql);

    System.out.println("codigo dao "+c.getCod_bus());
System.out.println(c.getImagen_bus());
String b ="";

  File file1 = new File("C:\\asientos.jpg");
            FileInputStream fis = new FileInputStream(file1);
      ps.setInt(1,c.getCod_bus());
      ps.setString(2,c.getPlaca());
      ps.setString(3, c.getMarca());
      ps.setString(4,c.getEstado());
     ps.setInt(5,c.getNroasientos());
      ps.setString(6,c.getDescripcion());
     ps.setBinaryStream(7, fis,(int)file1.length());

    ps.executeUpdate();

    }
public Bus buscarbus(Connection con,int codigo) throws SQLException{
    Bus bean = new Bus();
  String sql = "select * from bus where cod_bus=?";

        PreparedStatement ps = con.prepareStatement(sql);
        ps.setInt(1,codigo);

          ResultSet rs = ps.executeQuery();
        if(rs.next()){

          bean.setCod_bus(rs.getInt(1));
        bean.setPlaca(rs.getString(2));
        bean.setMarca(rs.getString(3));
        bean.setEstado(rs.getString(4));
         bean.setNroasientos(rs.getInt(5));
 bean.setDescripcion(rs.getString(6));
        }
        return bean;


}


 public ArrayList<Bus> Listabus(Connection con) throws SQLException{

          ArrayList<Bus> lista = new ArrayList<Bus>();

  String sql = "select * from bus";
        PreparedStatement ps = con.prepareStatement(sql);
          ResultSet rs = ps.executeQuery();
       while(rs.next()){
    Bus bean = new Bus();

      bean.setCod_bus(rs.getInt(1));
        bean.setPlaca(rs.getString(2));
       bean.setMarca(rs.getString(3));
       bean.setEstado(rs.getString(4));
           bean.setNroasientos(rs.getInt(5));
       bean.setDescripcion(rs.getString(6));
   
         lista.add(bean);
        }
        return lista;

}

















}
